/**
 * 
 */
package me.pplay.playtalk.activity.friends;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.BaseActivity;
import me.pplay.playtalk.adapter.CommonAdapter;
import me.pplay.playtalk.bean.Chat;
import me.pplay.playtalk.bean.CommonControlID;
import me.pplay.playtalk.callback.CommonAdapterOnClickCallBack;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.database.FriendDb;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.service.PlayTalkService;
import me.pplay.playtalk.util.BuddyInfoUtil;
import me.pplay.playtalk.util.LogManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author seward
 * 
 */
public class DiscoverActivity extends BaseActivity {
	TextView searchbyid, friendreq;
	ListView suggestionlist;
	private List<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
	private ListView list;
	private TpChat tpChat;
	private FriendDb dbFriend;
	private List<Chat> friendList;
	private CommonControlID holderId = new CommonControlID();
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Globals.SETADAPTER:
				setNewFriendRequest();
				break;
		}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_discover);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.btn_back));
		dbFriend	= new FriendDb(this);
		searchbyid = (TextView) findViewById(R.id.discover_searchbyid);
		friendreq = (TextView) findViewById(R.id.discover_newfriendsreq);
		suggestionlist = (ListView) findViewById(R.id.discover_list);
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		tpChat = appState.getTpChat();
		mData = getData();
		
		holderId.itemLayout = R.layout.layout_discoverlist;
		holderId.leftImgId = R.id.discover_img;
		holderId.rightImgId = R.id.discover_add;
		holderId.leftStrId = R.id.discover_title;
		holderId.leftStr1Id = R.id.discover_topic;
		CommonAdapter adapter = new CommonAdapter(DiscoverActivity.this, mData, holderId, new CommonAdapterOnClickCallBack() {

			@Override
			public void OnClickCallback(int pos, int ViewId, View v) {
				// TODO Auto-generated method stub
				LogManager.LogShow("DiscoverActivity", "OnClickCallback position " + pos);
				if(holderId.rightImgId == ViewId)
				{
					LinkedList<TPBuddyinfo> flist = tpChat.tp_chat_get_suggestion_data();
					if(flist!=null){
						TPBuddyinfo buddyinfo = flist.get(pos);
						if(buddyinfo!=null){
							int skyid = buddyinfo.getSkyid();
							buddyinfo = tpChat.tp_chat_getBuddyinfo(skyid);
							if(buddyinfo == null){
								LogManager.LogShow("DiscoverActivity", "OnClickCallback position send buddy");
								tpChat.tp_chat_add_buddy(BuddyInfoUtil.getAlias(Globals.myInfo),skyid, tpChat.tp_chat_getfriendsgid(), "");
							}else{
								showToast(getString(R.string.addbuddy_exist));
							}
						}
						
					}
				}
			}
		});
		list = (ListView) this.findViewById(R.id.discover_list); // 联系人ListView
		list.setAdapter(adapter);// 将数据适配器与Activity进行绑定

		clickEvents();

	}

	private void clickEvents() {
		searchbyid.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(DiscoverActivity.this, SearchByIDAcitivity.class);
				startActivity(intent);
			}
		});
		friendreq.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(friendList != null && friendList.size() > 0){
					Intent intent = new Intent(DiscoverActivity.this, FriendRquestResultActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("friendlist", (Serializable)friendList);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			}
		});
	}

	private List<Map<String, Object>> getData() {

		mData.clear();
		LinkedList<TPBuddyinfo> flist = tpChat.tp_chat_get_suggestion_data();
		if (flist != null) {
			for (TPBuddyinfo g : flist) {
				LogManager.LogShow("DiscoverActivity", "g.getName() " + g.getName());
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("leftStr", BuddyInfoUtil.getAlias(g));
				if (g.getSignature().length() > 0)
					map.put("leftStr1", g.getSignature());
				map.put("leftImg", R.drawable.friends_default);
				map.put("rightImg", R.drawable.addfriends);
				map.put("obj", g);
				mData.add(map);
			}
		}
		return mData;

		// List<Map<String, Object>> list = new ArrayList<Map<String,
		// Object>>();
		// Map<String, Object> map = new HashMap<String, Object>();
		// map.put("leftStr", "G1");
		// map.put("leftStr1", "google 1");
		// map.put("leftImg", R.drawable.friends_default);
		// map.put("rightImg", R.drawable.addfriends);
		// list.add(map);
		// map = new HashMap<String, Object>();
		// map.put("leftStr", "G2");
		// map.put("leftStr1", "google 2");
		// map.put("leftImg", R.drawable.friends_default);
		// map.put("rightImg", R.drawable.addfriends);
		// list.add(map);
		// map = new HashMap<String, Object>();
		// map.put("leftStr", "G3");
		// map.put("leftStr1", "google 3");
		// map.put("leftImg", R.drawable.friends_default);
		// map.put("rightImg", R.drawable.addfriends);
		// list.add(map);
		// return list;

	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		PlayTalkService.getInstance().setHandler(handler);
		PlayTalkService.getInstance().setIsDiscover(true);
		setNewFriendRequest();
		super.onStart();
	}

	@Override
	protected void onStop() {
		PlayTalkService.getInstance().setIsDiscover(false);
		super.onStop();
	}
	
	private void setNewFriendRequest(){
		friendList = dbFriend.queryList(3);
		friendreq.setText(friendList.size() + getString(R.string.discover_newfriendsreq));
	}
}
